package com.pristine.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pristine.dao.IConsumableDao;
import com.pristine.domain.ConsumableEntity;
import com.pristine.service.wsdl.ConsumableVO;
import com.pristine.service.wsdl.IConsumableService;
import com.pristine.util.EntityToVOConverter;
import com.pristine.util.VOToEntityConverter;

@Service("consumableService")
@Transactional
public class ConsumableServiceImpl implements IConsumableService{

	@Autowired
	private IConsumableDao consumableDao;
	
	@Override
	public Integer createConsumable(ConsumableVO consumableVO){
		ConsumableEntity consumable = VOToEntityConverter.convert(consumableVO);
		consumable.setCreatedOn(new Date());
		consumableDao.add(consumable);
		return consumable.getConsumableId();
	}
	
	@Override
	public Integer updateConsumable(ConsumableVO consumableVO){
		ConsumableEntity consumable = VOToEntityConverter.convert(consumableVO);
		consumable.setModifiedOn(new Date());
		consumableDao.update(consumable);
		
		return consumable.getConsumableId();
	}
	
	@Override
	public boolean deleteConsumable(Integer id){
		try{
			ConsumableEntity entity = consumableDao.find(id);
			consumableDao.remove(entity);
			return true;
		}catch(Exception e){
			return false;
		}
	}
	
	@Override
	public ConsumableVO getConsumableById(Integer id){
		return EntityToVOConverter.convert(consumableDao.find(id));
	}
	
	@Override
	public List<ConsumableVO> getAllConsumable() {
		
		List<ConsumableEntity> list = consumableDao.list();
		List<ConsumableVO> voList= new ArrayList<ConsumableVO>();
		if(CollectionUtils.isNotEmpty(list)){
			for(ConsumableEntity entity:list){
				voList.add(EntityToVOConverter.convert(entity));
			}
		}
		return voList;
	}
	
}
